package com.swsc.solarverse.common.util;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author anonymous
 * @Date 2021/6/10 8:56
 * @Version 1.0
 * @Description 正则表达式
 */
public class RegexUtils {
    /**
     * 大陆手机号
     */
    public static final String REGEX_MOBILEPHONE = "^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$";
    public static final String REGEX_TELEPHONE = "\\d{3}-\\d{8}|\\d{4}-\\d{7}";
    /**
     * 汉字
     */
    public static final String REGEX_CHINESE = "^[\\u4e00-\\u9fa5]{0,}$";
    /**
     * 正整数
     */
    public static final String REGEX_INT_POSITIVE = "^[1-9]\\d*$";
    /**
     * 负整数
     */
    public static final String REGEX_INT_NEGATIVE = "^-[1-9]\\d*$";
    /**
     * 整数
     */
    public static final String REGEX_INT = "^(-?[1-9]\\d*|0)$";

    public static final String REGEX_ORGAN_NAME = "^(?!\\s*$)[A-Za-z0-9._\\-\\(\\)\\u4e00-\\u9fa5\\（）\\s-]+$";

    public static final String REGEX_ALL_DATE_TIME = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";

    public static final String REGEX_ALL_DATE = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$";
    public static final String REGEX_TIME = "((((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
    public static final Pattern PATTERN_MOBILE = Pattern.compile(REGEX_MOBILEPHONE);
    public static final Pattern PATTERN_TELEPHONE = Pattern.compile(REGEX_TELEPHONE);
    public static final Pattern PATTERN_CHINESE = Pattern.compile(REGEX_CHINESE);
    public static final Pattern PATTERN_INT = Pattern.compile(REGEX_INT);
    public static final Pattern PATTERN_ORGAN_NAME = Pattern.compile(REGEX_ORGAN_NAME);

    public static final Pattern PATTERN_ALL_DATE_TIME = Pattern.compile(REGEX_ALL_DATE_TIME);

    public static final Pattern PATTERN_ALL_DATE = Pattern.compile(REGEX_ALL_DATE);
    public static final Pattern PATTERN_TIME = Pattern.compile(REGEX_TIME);
    /**
     * 数字 Pattern.compile("^-?[0-9]+");
     */
    public static final Pattern PATTERN_NUMERIC = Pattern.compile("^-?\\d+(\\.\\d+)?$");

    public static final Pattern PATTERN_WIN_DRIVE = Pattern.compile("^[a-zA-Z]:.*$");

    public static final Pattern PATTERN_STANDARD_STRING_CODE_PREFIX = Pattern.compile("\\d{2}-\\d{2}-\\d{2}-\\d{3}");
    public static final Pattern PATTERN_STANDARD_STRING_CODE = Pattern.compile("\\d{2}-\\d{2}-\\d{2}-\\d{3}-\\d{2}");

    /** 国能澄城-逆变器编码格式-提取子阵编码 */
    public static final Pattern PATTERN_INVERTER_SUBMATRIX_CODE_EXTRACT = Pattern.compile("([A-Za-z]\\d{2})(?=-\\d{3})");
    public static final Pattern PATTERN_STANDARD_SUBMATRIX_CODE_EXTRACT = Pattern.compile("\\b\\d{2}-\\d{2}-\\d{2}\\b");
    public static final Pattern PATTERN_STANDARD_SUBMATRIX_CODE_PREFIX = Pattern.compile("\\d{2}-\\d{2}-\\d{2}");
    //    public static final Pattern PATTERN_STANDARD_STRING_CODE_PREFIX = Pattern.compile("\\d{2}-\\d{2}-\\d{2}-\\d{3}");
//    public static final Pattern PATTERN_STANDARD_STRING_CODE = Pattern.compile("\\d{2}-\\d{2}-\\d{2}-\\d{3}-\\d{2}");
    public static final Pattern PATTERN_TASKSOURCE_SUBTASK_EXTRACT = Pattern.compile("SUBTASK:(\\d{21})");
    public static final Pattern PATTERN_AUTOTASK_PROGRESS_KEY_EXTRACT = Pattern.compile("AUTOTASK:PROGRESS:(\\d{19})");

    public static final Pattern PATTERN_DJI_IMAGE = Pattern.compile("DJI_\\d{14}(_\\d{3,5})_[TZV](.*\\.(JPG|jpg|JPEG|jpeg|PNG|png))");
    public static final Pattern PATTERN_DJI_IMAGE_CODE = Pattern.compile("DJI_\\d{14}_\\d{3,5}_[TZV]_?(.*?)(\\.JPG|\\.jpg|\\.JPEG|\\.jpeg|\\.PNG|\\.png)?$");

    public static final Pattern PATTERN_SQUARE_BRACKETS_IN_EXTRACT = Pattern.compile("\\[(.*?)\\]");

    public static final Pattern PATTERN_FORWARD_TRANSMISSION_FILENAME = Pattern.compile("^\\d{9}_(\\d{14})\\.txt$");
    public static final Pattern PATTERN_FORWARD_TRANSMISSION_FILENAME_TIMESTAMP_EXTRACT = Pattern.compile("_(\\d{14})\\.txt$");

    public static String replaceChineseCharacter(String src) {
        if (null != src && !"".equals(src)) {
            src = src.replace("，", ",");
            src = src.replace("、", ",");
            src = src.replace("；", ",");
            src = src.replace("。", ",");
        }
        return src;
    }

    public static String chineseEnglishCharacterConversion(String smsStr) {
        //去除空格和——
        String replace = smsStr.replace("——", "--");
        String smsTemp = replace.replaceAll("\\s", "");
        char[] chars = smsTemp.toCharArray();
        for (int i = 0; i < chars.length; i++) {

            if (chars[i] == '，') {
                chars[i] = ',';
                continue;
            }
            if (chars[i] == '。') {
                chars[i] = '.';
                continue;
            }
            if (chars[i] == '；') {
                chars[i] = ';';
                continue;
            }
            if (chars[i] == '？') {
                chars[i] = '?';
                continue;
            }
            if (chars[i] == '！') {
                chars[i] = '!';
                continue;
            }
            if (chars[i] == '、') {
                chars[i] = '\\';
                continue;
            }
            if (chars[i] == '’') {
                chars[i] = '\'';
                continue;
            }
            if (chars[i] == '‘') {
                chars[i] = '\'';
                continue;
            }
            if (chars[i] == '“') {
                chars[i] = '"';
                continue;
            }
            if (chars[i] == '“') {
                chars[i] = '"';
                continue;
            }
            if (chars[i] == '（') {
                chars[i] = '(';
                continue;
            }
            if (chars[i] == '）') {
                chars[i] = ')';
                continue;
            }
            if (chars[i] == '-') {
                chars[i] = '-';
                continue;
            }
            if (chars[i] == '【') {
                chars[i] = '[';
                continue;
            }
            if (chars[i] == '】') {
                chars[i] = ']';
                continue;
            }
            if (chars[i] == '《') {
                chars[i] = '<';
                continue;
            }
            if (chars[i] == '》') {
                chars[i] = '>';
                continue;
            }

        }

        return new String(chars);
    }

    /**
     * 整数
     *
     * @param src
     * @return
     */
    public static boolean isInt(String src) {
        return isNotEmpty(src) && PATTERN_INT.matcher(src).matches();
    }

    /**
     * 汉字
     *
     * @param src
     * @return
     */
    public static boolean isChinese(String src) {
        return isNotEmpty(src) && PATTERN_CHINESE.matcher(src).matches();
    }

    /**
     * 电话
     *
     * @param src
     * @return
     */
    public static boolean isPhone(String src) {
        return isTelephone(src) || isCellphone(src);
    }

    /**
     * 大陆手机号
     *
     * @param src
     * @return
     */
    public static boolean isCellphone(String src) {
        return isNotEmpty(src) && PATTERN_MOBILE.matcher(src).matches();
    }

    /**
     * 电话
     *
     * @param src
     * @return
     */
    public static boolean isTelephone(String src) {
        return isNotEmpty(src) && PATTERN_TELEPHONE.matcher(src).matches();
    }

    public static boolean isOrganName(String src) {
        return PATTERN_ORGAN_NAME.matcher(src).matches();
    }

    public static boolean matchAllDateTime(String src) {
        return null != src && PATTERN_ALL_DATE_TIME.matcher(src).matches();
    }

    public static boolean isDate(String src) {
        return null != src && PATTERN_ALL_DATE.matcher(src).matches();
    }

    public static boolean isTime(String src) {
        return null != src && PATTERN_TIME.matcher(src).matches();
    }


    private RegexUtils() {
    }

    /**
     * common-lang3包中的方法
     */
    public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }

    /**
     * common-lang3包中的方法
     */
    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * common-lang3包中的方法
     */
    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

    /**
     * common-lang3包中的方法
     */
    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNumeric(String str) {
        return PATTERN_NUMERIC.matcher(str).matches();
    }

    public static boolean hasDrive(String path) {
        if (null == path) {
            return false;
        }
        return PATTERN_WIN_DRIVE.matcher(path).matches();
    }

    public static boolean isStandardCodeSubmatrixPrefix(String src){
        if(null == src){
            return false;
        }
        return PATTERN_STANDARD_SUBMATRIX_CODE_PREFIX.matcher(src).find();
    }

    public static boolean isStandardCodeStringPrefix(String src) {
        return PATTERN_STANDARD_STRING_CODE_PREFIX.matcher(src).find();
    }

    public static boolean isStandardStringCode(String src) {
        return PATTERN_STANDARD_STRING_CODE.matcher(src).matches();
    }

    public static String removeParentheses(String input) {
        if (null == input) {
            return input;
        }
        // 使用正则表达式去除括号及括号内的内容
        return input.replaceAll("\\([^()]*\\)", "");
    }

    public static String sanitizeFileName(String fileName) {
        // 定义不允许在文件名称中出现的特殊字符
        String invalidChars = "[\\\\/:*?\"<>|]";
        return fileName.replaceAll(invalidChars, "");
    }
    public static String extractInverterSubmatrixCode(String deviceCode) {
        if (null != deviceCode && !"".equals(deviceCode.trim())) {
            Matcher matcher = PATTERN_INVERTER_SUBMATRIX_CODE_EXTRACT.matcher(deviceCode);
            while (matcher.find()) {
                return matcher.group(0);
            }
        }
        return null;
    }
    public static String extractSubmatrixCode(String deviceCode) {
        if (null != deviceCode && !"".equals(deviceCode.trim())) {
            Matcher matcher = PATTERN_STANDARD_SUBMATRIX_CODE_EXTRACT.matcher(deviceCode);
            while (matcher.find()) {
                return matcher.group(0);
            }
        }
        return null;
    }

    public static boolean matchSubTaskSource(String taskSource) {
        if (null == taskSource || "".equals(taskSource.trim())) {
            return false;
        }
        return PATTERN_TASKSOURCE_SUBTASK_EXTRACT.matcher(taskSource).matches();
    }

    public static String extractTaskSubNo(String taskSource) {
        if (null != taskSource && !"".equals(taskSource.trim())) {
            Matcher matcher = PATTERN_TASKSOURCE_SUBTASK_EXTRACT.matcher(taskSource);
            while (matcher.find()) {
                return matcher.group(1);
            }
        }
        return null;
    }

    public static boolean matchAutoTaskProgressKey(String autoTaskProgressKey) {
        if (null == autoTaskProgressKey || "".equals(autoTaskProgressKey.trim())) {
            return false;
        }
        return PATTERN_AUTOTASK_PROGRESS_KEY_EXTRACT.matcher(autoTaskProgressKey).matches();
    }

    public static String extractAutoTaskProgressKeyTaskNo(String autoTaskProgressKey) {
        if (null != autoTaskProgressKey && !"".equals(autoTaskProgressKey.trim())) {
            Matcher matcher = PATTERN_AUTOTASK_PROGRESS_KEY_EXTRACT.matcher(autoTaskProgressKey);
            while (matcher.find()) {
                return matcher.group(1);
            }
        }
        return null;
    }

    public static String removeFileNameNoSuffix(String fileName) {
        if (null == fileName || "".equals(fileName.trim())) {
            return fileName;
        }
        return fileName.replaceFirst("[.][^.]+$", "");
    }

    public static boolean matchDjiImageGroupName(String imageName1, String imageName2) {
        if (Objects.equals(imageName1, imageName2)) {
            return true;
        }
        if (null == imageName1 || null == imageName2) {
            return false;
        }
        String cleanedFileName1 = cleanDjiImageName(imageName1);
        String cleanedFileName2 = cleanDjiImageName(imageName2);
        return cleanedFileName1.equals(cleanedFileName2);
    }

    private static String cleanDjiImageName(String fileName) {
        Matcher m = PATTERN_DJI_IMAGE.matcher(fileName);
        if (m.find()) {
            return m.group(1) + m.group(2);
        }
        return fileName;
    }

    public static String extractDeviceCodeFromDjiImageName(String imageName) {
        if (null != imageName && !"".equals(imageName.trim())) {
            Matcher matcher = PATTERN_DJI_IMAGE_CODE.matcher(imageName);
            if (matcher.find()) {
                return null != matcher.group(1) ? matcher.group(1) : null;
            }
        }
        return null;
    }

    public static String extractSquareBracketsFirst(String src) {
        if (null != src) {
            Matcher matcher = PATTERN_SQUARE_BRACKETS_IN_EXTRACT.matcher(src);

            while (matcher.find()) {
                return matcher.group(1);
            }
        }
        return null;
    }

    public static boolean isForwardTransmissionFilename(String fileName) {
        if (null != fileName && !"".equals(fileName.trim())) {
            return PATTERN_FORWARD_TRANSMISSION_FILENAME.matcher(fileName).matches();
        }
        return false;
    }

    public static String extractForwardTransmissionFilenameTimestamp(String fileName) {
        if (null != fileName && !"".equals(fileName.trim())) {
            Matcher matcher = PATTERN_FORWARD_TRANSMISSION_FILENAME.matcher(fileName);
            while (matcher.find()) {
                return matcher.group(1);
            }
        }
        return "";
    }

    public static String removeChinese(String str) {
        // 定义中文字符的正则表达式
        String regex = "[\u4e00-\u9fa5]";
        // 使用replaceAll方法替换中文字符为空字符串
        return str.replaceAll(regex, "");
    }

}
